import { _decorator, AudioClip, AudioSource, Component, director, Node, resources } from 'cc';
import DatasSpace from './DatasSpace';
const { ccclass, property } = _decorator;

@ccclass('AudioMgr')
export class AudioMgr extends Component {

  public static _instance: AudioMgr = null
  public static get Instance() {
    if (null == this._instance) {
      this._instance = new AudioMgr();
    }
    return this._instance;
  }

  private _persistRootNode: Node = null;
  clipCache: { [name: string]: AudioClip } = {}

  start() {

  }

  update(deltaTime: number) {

  }

  loadClip(name: string) {

  }

  init() {
    if (this._persistRootNode) return; //避免切换场景初始化报错
    this._persistRootNode = new Node('audio');
    director.getScene()!.addChild(this._persistRootNode);
    director.addPersistRootNode(this._persistRootNode)
    this._persistRootNode.addComponent(AudioSource);

  }

  play(name: ENUM_CLIP | string = null, loop: boolean = false) {
    if (!DatasSpace.Instance.GameSound) return;
    if (name) {
      if (this.clipCache[name]) {
        const clip = this.clipCache[name];
        if (loop) {
          this._persistRootNode.getComponent(AudioSource)!.clip = clip;
          this._persistRootNode.getComponent(AudioSource)!.loop = true;
          this._persistRootNode.getComponent(AudioSource)!.play();
        } else {

          this._persistRootNode.getComponent(AudioSource)!.playOneShot(clip, 1);
        }
      } else {
        resources.load('audio/' + name, (err, data: AudioClip) => {
          if (err) {
            return console.error(err);
          } else {
            this.clipCache[name] = data;
            if (loop) {
              this._persistRootNode.getComponent(AudioSource)!.clip = data;
              this._persistRootNode.getComponent(AudioSource)!.loop = true;
              this._persistRootNode.getComponent(AudioSource)!.play();
            } else {
              this._persistRootNode.getComponent(AudioSource)!.playOneShot(data, 1);
            }
          }
        })
      }
    }
  }

  playMj(name: string = null) {
    if (!DatasSpace.Instance.GameSound) return;
    if (name) {
      if (this.clipCache[name]) {
        const clip = this.clipCache[name];
        this._persistRootNode.getComponent(AudioSource)!.playOneShot(clip, 1);
      } else {
        resources.load('audio/mj/' + name, (err, data: AudioClip) => {
          if (err) {
            return console.error(err);
          } else {
            this.clipCache[name] = data;
            this._persistRootNode.getComponent(AudioSource)!.playOneShot(data, 1);
          }
        })
      }
    }
  }

  stop(name: ENUM_CLIP | string = null) {
    this._persistRootNode.getComponent(AudioSource).stop();
    this._persistRootNode.getComponent(AudioSource)!.clip = null;
    this._persistRootNode.getComponent(AudioSource)!.loop = false;
  }


}

export enum ENUM_CLIP {
  Click = 'Click',
  Combie = 'Combie',
  CreateMahjon = 'CreateMahjong',
  Fail = 'Fail',
  Win = 'Win',
  CombineAndClick = 'CombineAndClick',
  Achievement = 'Achievement'
}

